
package com.ls.fw.mvc.struts.converter.sql;

import java.text.ParseException;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.ls.fw.commons.core.utils.date.DateTypeUtil;


/**
 * 
 * @author Defender
 * 2013-7-7 下午01:36:42
 */
public class TimeConverter extends StrutsTypeConverter {
	
	
	/**
	 * String转换Timestamp
	 *
	 */
	@Override
	public Object convertFromString(Map arg0, String[] value, Class arg2) {
		if ("".equals(((String[])value)[0]) || ((String[])value)[0] == null) {
			return null;
		}
		try {
			return DateTypeUtil.toTime(value[0]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Timestamp转换String 格式:yyyy-MM-dd hh:mm:ss
	 *
	 */
	@Override
	public String convertToString(Map arg0, Object timestamp) {
		if (timestamp == null || "null".equals(timestamp)) {
			return "";
		}
	 
		try {
			return DateTypeUtil.toString(timestamp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
	
}
